package com.guohe3.just.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guohe3.just.DO.Feedback;
import com.guohe3.just.common.enums.OriginEnum;
import com.guohe3.just.common.enums.ResultEnum;
import com.guohe3.just.common.execption.CustomException;
import com.guohe3.just.mapper.FeedbackMapper;
import com.guohe3.just.service.IFeedbackService;
import com.guohe3.just.task.AsyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 浦希成
 * @since 2018-12-30
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {
    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private AsyncTask asyncTask;

    @Override
    public void create(Feedback feedback) {
        List<Feedback> feedbackBDs = feedbackMapper.selectList(new QueryWrapper<Feedback>().eq("content", feedback.getContent()).eq("name", feedback.getName()));
        if (feedbackBDs == null || feedbackBDs.size() == 0) {
            feedback.setTime(new Date());
            feedbackMapper.insert(feedback);
            String text = "姓名：%s\n联系方式：%s\n来源：%s\n内容：%s";
            //异步发送邮件
            asyncTask.sendMail(String.format(text, feedback.getName(), feedback.getContact(), OriginEnum.getValueByCode(feedback.getOrigin()), feedback.getContent()));
        } else {
            throw new CustomException(ResultEnum.REPEATED_ERROR);
        }


    }

}
